﻿using System;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;
using System.Collections.Generic;

namespace Sinbiota.Web.Controllers
{
    public class MethodController : BaseController
    {
        private readonly IMethodPersistenceServices _methodPersistenceServices;

        public MethodController(IMethodPersistenceServices methodPersistenceServices)
        {
            _methodPersistenceServices = methodPersistenceServices;
        }

        //
        // GET: /Method/
        [Authorize]
        [ImportMessagesFromTempData]
        public ActionResult Index()
        {
            return View(_methodPersistenceServices.FindAll());
        }

        [Authorize]
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {
            return View(_methodPersistenceServices.FindByNameLike(collection[SearchBoxName]));
        }

        //
        // GET: /Method/Details/5
        [Authorize]
        [ImportMessagesFromTempData]
        public ActionResult Details(int id)
        {
            var method = _methodPersistenceServices.FindById(id);
            return method == null ? View("NotFound") : View(method);
        }

        //
        // GET: /Method/Create
        [Authorize]
        [ImportMessagesFromTempData]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Method/Create
        [Authorize]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Create(Method method)
        {
            var newMethod = new Method();
            TryUpdateModel(newMethod);
            if (ModelState.IsValid)
            {
                _methodPersistenceServices.Save(newMethod);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(method);
        }

        //
        // GET: /Method/Edit/5
        [Authorize]
        [ImportMessagesFromTempData]
        public ActionResult Edit(int id)
        {
            var method = _methodPersistenceServices.FindById(id);
            return method == null ? View("NotFound") : View(method);
        }

        //
        // POST: /Method/Edit/5
        [Authorize]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Edit(int id, Method method)
        {
            var editMethod = _methodPersistenceServices.FindById(id);
            TryUpdateModel(editMethod);
            if (ModelState.IsValid)
            {
                _methodPersistenceServices.Update(editMethod);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(method);
        }

        //
        // GET: /Method/Delete/5
        [Authorize]
        [ImportMessagesFromTempData]
        public ActionResult Delete(int id)
        {
            var method = _methodPersistenceServices.FindById(id);
            return method == null ? View("NotFound") : View(method);
        }

        //
        // POST: /Method/Delete/5
        [Authorize]
        [HttpPost, ExportMessagesToTempData]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var deleteMethod = _methodPersistenceServices.FindById(id);
            if (deleteMethod == null) return View("NotFound");
            _methodPersistenceServices.Delete(deleteMethod);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }
    }
}
